package util.ir;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.LockObtainFailedException;

import util.io.FileInput;

public class IndexMixLanguageModel {

	/**
	 * @param args
	 */

	static final File INDEX_DIR = new File("index");
	private static boolean deleting = false; // true during deletion pass
	private static IndexReader reader; // existing index
	private static IndexWriter writer; // new index being built
	String index_path = "/home/sergio/index/LM_trigrams";

	static String index = "";

	
	
	private static void indexbigramsSimple(String path)
			throws CorruptIndexException, LockObtainFailedException,
			IOException {

		writer = new IndexWriter(index, new WhitespaceAnalyzer(), true);

		FileInput in = new FileInput(path);
		String line = in.readString();

		while (line != null) {

			Document doc = new Document();

			
			//adults
			/*
			 *---	
			 *area	
			 *1	
			 *17	
			 *901	
			 *2	
			 *613	
			 *12125 
			 */
			
			
			String fields[] = line.split("\t");
			String type = fields[0];
			
			String a = fields[1];
			String b = fields[2];

			String freq = fields[3];
			String freq_1 = fields[4];
			String freq_2 = fields[5];
			String freq_b = fields[6];
			String freq_1_b = fields[7];
			String freq_2_b = fields[8];
			
			String bigram = a + "\t" + b;

			doc.add(new Field("bigram", bigram, Field.Store.YES,Field.Index.ANALYZED));

			// store frequencies
			doc.add(new Field("f_b", freq, Field.Store.YES,
					Field.Index.NOT_ANALYZED));

			doc.add(new Field("f_1", freq_1, Field.Store.YES,
					Field.Index.NOT_ANALYZED));

			doc.add(new Field("f_2", freq_2, Field.Store.YES,
					Field.Index.NOT_ANALYZED));
			
			
			doc.add(new Field("f_1_b", freq_1_b, Field.Store.YES,
					Field.Index.NOT_ANALYZED));

			doc.add(new Field("f_2_b", freq_2_b, Field.Store.YES,
					Field.Index.NOT_ANALYZED));
			
			doc.add(new Field("f_b_b", freq_b, Field.Store.YES,
					Field.Index.NOT_ANALYZED));
			doc.add(new Field("type", type, Field.Store.YES,
					Field.Index.NOT_ANALYZED));
			
			System.out.println("indexing mix bigrams.. " + line);
			writer.addDocument(doc); // add docs unconditionally

			line = in.readString();

		}
		System.out.println("Optimizing...");
		writer.optimize();
		writer.close();
		System.out.println("done!");
	}



	private static void indextrigramsSimple(String path)
			throws CorruptIndexException, LockObtainFailedException,
			IOException {

		writer = new IndexWriter(index, new WhitespaceAnalyzer(), true);

		FileInput in = new FileInput(path);
		String line = in.readString();

		while (line != null) {

		/*
		 * adults
		 * 	0-1	
		 * aston	
		 * villa	
		 * 1	
		 * 1	
		 * 16	
		 * 1	
		 * 1	
		 * 1	
		 * 147	
		 * 1
		 *  	
		 */
		 
		 
		
			Document doc = new Document();
//kids	cool	japan	yum	1	15	1	1	1	6521	12	21
			String fields[] = line.split("\t");
			String type = fields[0];
			String a = fields[1];
			String b = fields[2];
			String c = fields[3];
			String freq = fields[4];
			String freq_12 = fields[5];
			String freq_23 = fields[6];
			String freq_13 = fields[7];
			
			
			String freq_b = fields[8];
			String freq_12_b = fields[9];
			String freq_23_b = fields[10];
			String freq_13_b = fields[11];
			
		
			String trigram =  a+ "\t" + b + "\t" + c;
			
			
			doc.add(new Field("trigram", trigram, Field.Store.YES,Field.Index.ANALYZED));

			doc.add(new Field("type", type, Field.Store.YES,	Field.Index.NOT_ANALYZED));
			// store frequencies
			doc.add(new Field("f_t", freq, Field.Store.YES,	Field.Index.NOT_ANALYZED));

			

			doc.add(new Field("f_12", freq_12, Field.Store.YES,	Field.Index.NOT_ANALYZED));

			doc.add(new Field("f_23", freq_23, Field.Store.YES,	Field.Index.NOT_ANALYZED));

			doc.add(new Field("f_13", freq_13, Field.Store.YES,	Field.Index.NOT_ANALYZED));

			
			doc.add(new Field("f_t_b", freq_b, Field.Store.YES,	Field.Index.NOT_ANALYZED));

			

			doc.add(new Field("f_12_b", freq_12_b, Field.Store.YES,	Field.Index.NOT_ANALYZED));

			doc.add(new Field("f_23_b", freq_23_b, Field.Store.YES,	Field.Index.NOT_ANALYZED));

			doc.add(new Field("f_13_b", freq_13_b, Field.Store.YES,	Field.Index.NOT_ANALYZED));
			
			
			System.out.println("indexing mix trigrams.. " + line);
			writer.addDocument(doc); // add docs unconditionally

			line = in.readString();

		}
		System.out.println("Optimizing...");
		writer.optimize();
		writer.close();
		System.out.println("done!");
	}

	
	public static String createOrQuery(String fields[], String seeds[]) {

		String special_seed = "";

		for (int i = 0; i < seeds.length; i++) {

			for (int j = 0; j < fields.length; j++) {

				special_seed = special_seed + fields[j] + ":" + seeds[i] + " ";
			}
		}

		special_seed = "(" + special_seed.trim() + ")";

		return special_seed;
	}

	public static void trigramSearch(String seeds[], String t[], String index,
			String fields[], HashSet<String> set) throws CorruptIndexException,
			IOException, ParseException {

		IndexSearcher searcher = new IndexSearcher(index);

		Analyzer analyzer = new StandardAnalyzer();
		QueryParser queryParser = new QueryParser("<default field>", analyzer);
		// <default field> is the field that QueryParser will search if you
		// don't
		// prefix it with a field.

		String special_seed = createOrQuery(fields, seeds);
		String special_t = createOrQuery(fields, t);

		String q_extended = special_t + " AND " + special_seed;
		Hits hits = searcher.search(queryParser.parse(q_extended));

		for (int i = 0; i < hits.length(); i++) {

			Document doc = hits.doc(i);

			String r = "";
			for (int j = 0; j < fields.length; j++) {
				String f = fields[j];

				String temp = doc.get(f);
				r = r + "\t" + temp;

			}

			r = r + "\t" + doc.get("f");
			System.out.println(r.trim());

		}

		BooleanQuery booleanQuery = new BooleanQuery();

		searcher.close();

	}

	public static void standardSearch(String question[], String field[],
			String index, HashSet<String> set) throws ParseException,
			CorruptIndexException, IOException {

		IndexSearcher searcher = new IndexSearcher(index);
		BooleanQuery booleanQuery = new BooleanQuery();

		for (int i = 0; i < question.length; i++) {
			Query query1 = new TermQuery(new Term(field[i], question[i]));
			booleanQuery.add(query1, BooleanClause.Occur.SHOULD);
		}
		// Use BooleanClause.Occur.MUST instead of BooleanClause.Occur.SHOULD
		// for AND queries
		Hits hits = searcher.search(booleanQuery);

		System.out.println("> "); // start with '> '

		for (int i = 0; i < hits.length(); i++) {

			Document doc = hits.doc(i);

			String r = "";
			for (int j = 0; j < field.length; j++) {
				String f = field[j];

				String temp = doc.get(f);
				r = r + "\t" + temp;

			}

			r = r + "\t" + doc.get("f");
			System.out.println(r.trim());

		}

		System.out.println("\n" + hits.length() + " matching documents");

		searcher.close();

	}

	// static IndexSearcher searcher=null;

	/**
	 * 1 index/query 2 index path 3 query
	 */

	static public void main(String[] args) throws CorruptIndexException,
			LockObtainFailedException, IOException, ParseException {
		// TODO Auto-generated method stub

		String index_path = args[1];
		String source = args[2];
		String type = args[3];

		index = index_path;

		if (type.equals("3"))
			indextrigramsSimple(source);
		else
			indexbigramsSimple(source);

	}

}
